package shellstation;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.util.ArrayList;
import java.sql.ResultSet;

public class AddProductView implements ActionListener {

    private JButton submit;
    private int z = 1;
    private int m = 0;
    private static int a = 0;
    private JLabel productName;
    private JLabel productDesc;
    private JLabel productSize;
    private JLabel productPrice;
    private JLabel productStock;
    private JLabel productStatus;
    
    private JLabel NewSize;
    
     private JLabel NewUnitLabel;

    private JTextField pName;
    private JTextField pDesc;
    private JTextField pSize;
    private JTextField pPrice;
    private JTextField pStock;
    private JTextField newSize;
    
    private ArrayList listSize;
    private JComboBox SizeList;
    
    private JComboBox box2;
    private JComboBox statusbox;
    private JTextField newUnit;
    
    private JLabel name2;
   private static String[] status = {"Active", "Inactive"};
   private static ArrayList sizeName;
  // private static String[] sizeName2 = {"mL", "L", "pail", "g", "kg"};
      
    public void AddProductView(){

        ProductModel pr = new ProductModel();
        listSize = pr.getlistSize();
        sizeName = pr.getSizeUnit();
        productName = new JLabel("Product Name:");
        productName.setBounds(80, 40, 200, 20 );
       
        productStatus = new JLabel("Status:");
        productStatus.setBounds(80, 90, 200, 20 );
        
        productSize = new JLabel("Size:");
        productSize.setBounds(80, 140, 200, 20 );
        
        productPrice = new JLabel("Price:");
        productPrice.setBounds(80, 240, 200, 20 );
   
        productStock = new JLabel("Stock:");
        productStock.setBounds(80, 290, 200, 20);
       
        submit=new JButton("Add Product");
        submit.setBounds(130, 340, 200, 25 );
        submit.addActionListener(this);

        pName = new JTextField("",10);
        pName.setBounds(200, 40, 180, 25 );
        
       statusbox = new JComboBox(status);
        statusbox.setBounds(200, 90, 180, 25);
        
        box2 = new JComboBox(listSize.toArray());
        box2.setBounds(200, 140, 180, 25);
        
        newSize = new JTextField("",10);
        newSize.setBounds(80, 190, 100, 25 );
        
        SizeList = new JComboBox(sizeName.toArray());
        SizeList.setBounds(200, 190, 80, 25);
                
        
        NewSize = new JLabel("*Input new size here");
        NewSize.setBounds(80, 170, 200, 20 );
        
        NewUnitLabel = new JLabel("*New unit");
        NewUnitLabel.setBounds(300, 170, 200, 20 );
        
        
        newUnit = new JTextField("",10);
        newUnit.setBounds(300, 190, 80, 25 );
        
        
        pPrice = new JTextField("",10);
        pPrice.setBounds(200, 240, 180, 25 );
        
        pStock = new JTextField("",10);
        pStock.setBounds(200, 290, 180, 25 );
       
       
        

    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource()== submit){
         String n = pName.getText();
         String p = pPrice.getText();
         String t = pStock.getText();
         String s = box2.getSelectedItem().toString();
         String o = SizeList.getSelectedItem().toString();
         String nSize = newSize.getText();
         String nUnit = newUnit.getText();
         String sb = statusbox.getSelectedItem().toString();
         if(nSize.isEmpty())
             s = box2.getSelectedItem().toString();
         else {
             try {
         int x = Integer.parseInt(nSize);
         if (x <= 0) {
             JOptionPane.showMessageDialog(null,"Size must not have negative values");
             m=1;
         }
         else if(nUnit.isEmpty())
         s = nSize + " " +o;
         else{
             ProductModel sI = new ProductModel();
             ResultSet c;
             c = sI.getSizeID(nUnit);
             if(c==null){
             s = nSize + " " + nUnit;
             
             }
             else {
                 JOptionPane.showMessageDialog(null,"Unit of measure already exists");
                 m=1;
                 newUnit.setText("");
             }
         }
            }
        catch(NumberFormatException nFE) {
        JOptionPane.showMessageDialog(null,"Text box for size must only have numbers");
         m=1;}
         
         }
         if(n.isEmpty() || p.isEmpty() || t.isEmpty())
             JOptionPane.showMessageDialog(null, "Missing Values");
         else if(t.isEmpty() == false || p.isEmpty() == false){
             try {
         int x = Integer.parseInt(t);
         if(x<0) {
                m=1;
            JOptionPane.showMessageDialog(null,"Stock input must be a positive number");
            }
            }
        catch(NumberFormatException nFE) {
        JOptionPane.showMessageDialog(null,"Stock input must be a number");
         m=1;}
           try {

                    Float f = Float.valueOf(pPrice.getText());
                    if(f<0) {
                m=1;
            JOptionPane.showMessageDialog(null,"Price input must be a positive number");
            }
                    
                } catch (NumberFormatException nFE) {
                  JOptionPane.showMessageDialog(null,"Price input must be a number");
                m=1;}
           ProductController pI = new ProductController();
           
           ResultSet rm = pI.getProductCopy(n, s);
            if(rm != null) {
                JOptionPane.showMessageDialog(null, "Product already exists");
                pName.setText("");
                pPrice.setText("");
                pStock.setText("");
                newSize.setText("");
                newUnit.setText("");
                m=1;
            }
         if((!n.isEmpty() || !p.isEmpty() || !t.isEmpty()) && m ==0)
         this.z = JOptionPane.showConfirmDialog(null, "Are you sure you want to add this product?", "Confirm addition of product", JOptionPane.YES_NO_OPTION);
         if(z == 0){
            JOptionPane.showMessageDialog(null, "Product has been added!");
            ProductModel ap = new ProductModel();
            ap.getAddProduct(n, sb, s, p, t);
            ProductController nU = new ProductController();
             nU.getAddSizeUnit(nUnit);
         }
        
         }
        m=0; 
        a=1;

        }
    }

    public JLabel getProductName(){
        return productName;
    }

    public JLabel getProductDesc(){
        return productDesc;
    }

    public JLabel getProductSize(){
        return productSize;
    }

    public JLabel getNewSize(){
        return NewSize;
    }
    
    public JLabel getProductPrice(){
        return productPrice;
    }

    public JLabel getProductStock(){
        return productStock;
    }
    
     public JLabel getUnitLabel(){
        return NewUnitLabel;
    }
    
     public JLabel getProductStatus(){
        return productStatus;
    }

    public JTextField getpName(){
        return pName;
    }

    public JTextField getNewUnit(){
        return newUnit;
    }

    public JComboBox getbox2(){
        return box2;
    }

    public JTextField getpPrice(){
        return pPrice;
    }

    public JTextField getpStock(){
        return pStock;
    }
    
 public JTextField getnewSize(){
        return newSize;
    }
 
     public JComboBox getStatusBox(){
        return statusbox;
    }
     
    public JButton getsubmit(){
        return submit;
    }
    
    public JComboBox getSizeList(){
        return SizeList;
    }
   

}
